﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO;
using System.Text.RegularExpressions;

namespace PlateNumber {
    public partial class ValidationForm : Form {
        public ValidationForm() {
            InitializeComponent();
        }

        [DllImport("AspriseOCR.dll", EntryPoint = "OCR")]
        public static extern IntPtr OCR(string file, int type);

        [DllImport("AspriseOCR.dll", EntryPoint = "OCRpart")]
        static extern IntPtr OCRpart(string file, int type, int startX, int startY, int width, int height);

        [DllImport("AspriseOCR.dll", EntryPoint = "OCRBarCodes")]
        static extern IntPtr OCRBarCodes(string file, int type);

        [DllImport("AspriseOCR.dll", EntryPoint = "OCRpartBarCodes")]
        static extern IntPtr OCRpartBarCodes(string file, int type, int startX, int startY, int width, int height);


        public Image ValidationImage { private get; set; }
        public string ValidationCode { get; private set; }

        private string _imageFileName = Application.StartupPath + "\\" + Guid.NewGuid().ToString() + ".jpg";

        private void ValidationForm_Load(object sender, EventArgs e) {
            try {
                Bitmap bmp = new Bitmap(ValidationImage);

                Color darkest = Color.White;
                for (int x = 0; x < bmp.Width; ++x) {
                    for (int y = 0; y < bmp.Height; ++y) {
                        Color current = bmp.GetPixel(x, y);
                        if (current.GetBrightness() < darkest.GetBrightness()) {
                            darkest = current;
                        }
                    }
                }
                for (int x = 0; x < bmp.Width; ++x) {
                    for (int y = 0; y < bmp.Height; ++y) {
                        Color current = bmp.GetPixel(x, y);
                        if (current.GetBrightness() > darkest.GetBrightness() + 0.25) {
                            // 看看当前点周围的8个点中是否有存在深色点
                            int[] cx = { -1, 0, 1, 1, 1, 0, -1, -1 };
                            int[] cy = { -1, -1, -1, 0, 1, 1, 1, 1 };
                            int count = 0;
                            for (int i = 0; i < cx.Length; ++i) {
                                if (x + cx[i] >= 0 && x + cx[i] < bmp.Width &&
                                    y + cy[i] >= 0 && y + cy[i] < bmp.Height) {
                                    if (bmp.GetPixel(x + cx[i], y + cy[i]).GetBrightness() == darkest.GetBrightness()) {
                                        ++count;
                                    }
                                }
                            }

                            if (count < 3) {
                                bmp.SetPixel(x, y, Color.White);
                            }
                            if (count == 8) {
                                bmp.SetPixel(x, y, darkest);
                            }
                        }
                    }
                }
                
                pictureBox1.Image = bmp;
                textBox1.Focus();

                bmp.Save(_imageFileName, ImageFormat.Jpeg);
                textBox1.Text = Regex.Replace(
                    Marshal.PtrToStringAnsi(OCR(_imageFileName, -1)).Substring(0, 4),
                    "[^a-zA-Z0-9]*", "");
            }
            catch (Exception ex) {
                //MessageBox.Show(ex.ToString());
            }
        }

        private void btnOK_Click(object sender, EventArgs e) {
            try {
                ValidationCode = textBox1.Text.Trim();

                File.Delete(_imageFileName);

                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            catch {
            }
        }
    }
}
